<template>
  <BoTable
    :columns="columns"
    :request="_getPreAfterHealthList"
    ref="tableRef"
    row-key="msgId"
    show-index
  >
  </BoTable>
  <!-- 预览pdf -->
  <BoPreviewPdf
    title="预览PDF"
    :iframe-url="pdfUrl"
    :loading="loading"
    v-model:open="visible"
  ></BoPreviewPdf>
</template>
<route lang="json">
{
  "name": "pre-earthquake-structure",
  "meta": {
    "title": "震前后结构物风险评估分析"
  }
}
</route>
<script setup lang="tsx">
import { computed, ref } from 'vue';
import { getDeviceProjectSelect } from '@/api';
import { listToEnum, listToOption } from '@boeyo/shared';
import { BoPreviewPdf, BoTable, BoTableProps } from '@boeyo/ui';
import { message } from 'ant-design-vue';
import { omit } from 'lodash-es';
import {
  getHealthReport,
  getPreAfterHealthList,
  getReGenerateReport,
} from './api';

const tableRef = ref();

const projectOptions = ref<any[]>([]);
const projectEnum = ref<Record<string, any>>({});

const init = () => {
  getDeviceProjectSelect().then(({ data }) => {
    projectOptions.value = listToOption(data, 'name', 'projectId');
    projectEnum.value = listToEnum(data, 'name', 'projectId');
  });
};
init();

const _getPreAfterHealthList = async (params: any) => {
  params.projectName = projectEnum.value[params.projectName]?.text;
  params.startTime = params.timeRange?.[0] || '';
  params.endTime = params.timeRange?.[1] || '';
  params = omit(params, ['timeRange']);
  return await getPreAfterHealthList(params);
};

const columns = computed<BoTableProps['columns']>(() => [
  {
    title: '报告名称',
    dataIndex: 'reportName',
    hideInSearch: true,
  },
  {
    title: '所属项目',
    dataIndex: 'projectName',
    valueEnum: projectEnum.value,
    valueType: 'select',
  },
  {
    title: '事件范围',
    dataIndex: 'timeRange',
    valueType: 'dateRange',
    hideInTable: true,
  },
  {
    title: '生成时间',
    dataIndex: 'reportTime',
    hideInSearch: true,
  },
  {
    title: '操作',
    dataIndex: 'operation',
    hideInSearch: true,
    fixed: 'right',
    actions: [
      {
        text: '重新生成报告',
        onClick(record) {
          onReGenerate(record);
        },
      },
      {
        text: '预览',
        onClick(record) {
          onPreview(record);
        },
      },
      {
        text: '下载',
        onClick(record) {
          onDownload(record);
        },
      },
    ],
  },
]);

const loading = ref(true);
const visible = ref(false);
const pdfUrl = ref('');
const onPreview = (record: any) => {
  loading.value = false;
  getHealthReport(
    'pdf',
    {
      msgId: record.msgId,
      reportName: record.reportName,
    },
    true,
  ).then((res: any) => {
    pdfUrl.value = window.URL.createObjectURL(
      new Blob([res.data], { type: 'application/pdf' }),
    );
    loading.value = true;
  });
  visible.value = true;
};

const onDownload = async (record: any) => {
  await getHealthReport(
    'pdf',
    {
      msgId: record.msgId,
      reportName: record.reportName,
    },
    false,
  );
  message.success('下载成功');
};

/**
 * 生成报告
 */
const onReGenerate = async (record: any) => {
  if (!record.msgId) return message.info('请选择一条记录');
  await getReGenerateReport(record.msgId);
  message.success('重新生成成功');
};
</script>

<style lang="scss" scoped></style>
